import json
import os.path

import openpyxl
from openpyxl.workbook import Workbook
from openpyxl.worksheet.worksheet import Worksheet

my_path = r"D:\work\pythonProject"
class Dofile:
#字典转json格式字符串
    def DIctjsonstr(self,d):
        s=json.dumps(d)
        print(s,type(s))
        return s

#json字符串转字典
    def JsontoDictstr(self,jsons):
        d = json.loads(jsons)
        print(d,type(d))
        return d

#字典内容保存为json文件
    def DicteoJsonfile(self,d,fname):
        filename = os.path.join(my_path,fname)
        with open(filename,mode='a',encoding='utf-8') as f:
            json.dump(d,f)
    def doExcelread(self, fname):
        fileName = os.path.join(my_path,fname)

        wk:Workbook=openpyxl.load_workbook(fileName)
        st:Worksheet=wk["ac"]
        print(st,type(st))
        ss=st.iter_rows(values_only=True)
        print(ss)
        print(range(10))
        l=[i for i in ss]
        print(f"{l}")
        return l
    def doExcelreaddiv(self, fname,stname,minrow=None,maxrow=None,mincol=None,maxcol=None):
        fileName = os.path.join(my_path,fname)

        wk:Workbook=openpyxl.load_workbook(fileName)#获取工作簿对象
        st:Worksheet=wk[stname]#通过工作簿对象获取sheet对象
        print(st,type(st))
        ss=st.iter_rows(min_row=minrow,
                        max_row=maxrow,
                        min_col=mincol,
                        max_col=maxcol,
                        values_only=True)#获取sheet中的值
        l=[i for i in ss]
        print(f"{l}")
        return l
#json文件提取并转字典
    def doExcelwrite(self,fname,stname,ss):
        fileName = os.path.join(my_path,fname)
        wk=openpyxl.Workbook()
        st=Worksheet=wk.create_sheet(stname,1)
        st.cell(1,1,ss)
        wk.save(fileName)
if __name__ == '__main__':
    df=Dofile()
    # d = {"name":"mmp","age":123}
    # df.DicteoJsonfile(d,"myjson.json")
    # df.doExcelread("apt.xlsx")
    # df.doExcelreaddiv("apt.xlsx",'Sheet1',2,3,4,5)
    df.doExcelwrite("wdf.xlsx",'ntm',123,)

